﻿Imports EVOLTA.Promocion.Bl.Be
Imports EVOLTA.Promocion.Bl.Bc

Partial Class mantenedores_notaria
    Inherits System.Web.UI.Page

    Private notarias As List(Of Enotaria) = Nothing
    Dim expression As String = Nothing
    Dim direction As SortDirection = SortDirection.Ascending

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.CacheControl = "no-cache"
        Response.AddHeader("Pragma", "no-cache")
        Response.Expires = -1
        '***********************************************************'
        btnAgregar.Attributes.Add("onclick", "MuestraPanel(); return false;")
        If Not IsPostBack Then
            cargarnotarias()
            cargarEstado()
        End If
    End Sub
    Protected Sub btnBuscar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        gvwLista.DataSource = Buscar(txtBuscar.Value.ToUpper.Trim)
        gvwLista.DataBind()
    End Sub
    Private Function Buscar(ByVal filtro As String) As IEnumerable(Of Enotaria)
        Dim Lnotaria As System.Collections.Generic.IEnumerable(Of Enotaria) = Nothing
        notarias = Session("lista")
        If Not notarias Is Nothing Then
            If filtro.Length > 0 Then
                Dim x As Int32 = Asc(filtro.Substring(0, 1).Trim)
                If x >= 48 AndAlso x <= 57 Then
                    filtro = CType(filtro, Int32)
                    Lnotaria = From v In notarias _
                                Where v.idNotaria = filtro _
                                Select v
                    Return Lnotaria
                Else
                    Lnotaria = From v In notarias _
                                Where v.descNotaria Like "*" + filtro + "*" _
                                Select v
                End If
            Else
                Lnotaria = From v In notarias _
                            Where v.descNotaria Like "*" + filtro + "*" _
                            Select v
            End If
        End If
        Return Lnotaria
    End Function
    Protected Sub lnkBtn_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) _
    Handles lnkBtnCodigo.Command, lnkBtnNotaria.Command, lnkBtnNombre.Command, lnkBtnTelefono.Command, lnkBtnEstado.Command
        Dim notaria As IEnumerable(Of Enotaria) = Buscar(txtBuscar.Value.ToUpper.Trim)

        If Not notaria Is Nothing Then
            Select Case e.CommandName
                Case "idNotaria"
                    Dim List = From d In notaria _
                               Order By d.idNotaria _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
                Case "desNotaria" : expression = e.CommandArgument
                    Dim List = From d In notaria _
                               Order By d.descNotaria _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
                Case "NombresCompletos" : expression = e.CommandArgument
                    Dim List = From d In notaria _
                               Order By d.NombresCompletos _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
                Case "telefono" : expression = e.CommandArgument
                    Dim List = From d In notaria _
                               Order By d.telefono _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
                Case "desEstado" : expression = e.CommandArgument
                    Dim List = From d In notaria _
                               Order By d.desEstado _
                               Select d
                    gvwLista.DataSource = List
                    gvwLista.DataBind()
            End Select
        End If
    End Sub
    Private Sub cargarnotarias()
        notarias = Rnotaria.SeleccionarTodo(Me.Master.descEmpresa, False)
        Session("lista") = notarias
        If Not notarias Is Nothing Then
            gvwLista.DataSource = notarias
            gvwLista.DataBind()
        End If
    End Sub
    Private Sub cargarEstado()
        Dim Eestado As New Ecommons
        selEstado.DataSource = Eestado.estado
        selEstado.DataTextField = "descripcion"
        selEstado.DataValueField = "codigo"
        selEstado.DataBind()
    End Sub
    Protected Sub gvwLista_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwLista.PageIndexChanging
        'Me.gvwLista.PageIndex = e.NewPageIndex
        'Me.gvwLista.DataBind()
    End Sub
    Protected Sub gvwLista_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
    Handles gvwLista.RowCommand
        Select Case e.CommandName
            Case "Edit"
                'Dim id As String = e.CommandArgument
            Case "Delete"
                Dim pNotaria As Enotaria = New Enotaria
                With pNotaria
                    .idNotaria = e.CommandArgument
                    .idUsuario = Me.Master.idUsuario
                End With
                Rnotaria.Eliminar(Me.Master.descEmpresa, pNotaria)
                cargarnotarias()
        End Select
    End Sub
    Protected Sub gvwLista_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
    Handles gvwLista.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("OnMouseOver", "Resaltar_On(this);")
            e.Row.Attributes.Add("OnMouseOut", "Resaltar_Off(this);")
            e.Row.Attributes("OnClick") = Page.ClientScript.GetPostBackClientHyperlink(Me.gvwLista, "Select$" + e.Row.RowIndex.ToString)
            e.Row.Style.Add("cursor", "hand")
        End If
    End Sub
    Protected Sub gvwLista_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvwLista.RowDeleting
        'gvwLista.Visible = countList > 0
    End Sub
    Protected Sub gvwLista_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvwLista.RowEditing
        Dim id As Int32 = CType(CType(gvwLista.Rows(e.NewEditIndex).FindControl("lnkCodigo"), LinkButton).Text, Int32)
        notarias = Session("lista")
        Dim Lnotaria = From n In notarias _
                            Where n.idNotaria.ToString = id _
                            Select n
        lblTitulo.Text = "Notaria [Modificar]"
        lblTitulo.ToolTip = "Modificar"

        txtId.Text = Lnotaria.ElementAt(0).idNotaria
        txtNotaria.Value = Lnotaria.ElementAt(0).descNotaria
        txtContacto.Value = Lnotaria.ElementAt(0).nomContacto
        txtapePaterno.Value = Lnotaria.ElementAt(0).apePaterContac
        txtapeMaterno.Value = Lnotaria.ElementAt(0).apeMaterContac
        txtTelefonoFijo.Value = Lnotaria.ElementAt(0).telefono
        selEstado.SelectedIndex = Lnotaria.ElementAt(0).estado

        mpeDetalle.Show()
    End Sub
    Protected Sub btnGuardar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If txtNotaria.Value.Trim.Length > 0 AndAlso txtContacto.Value.Trim.Length > 0 AndAlso txtapeMaterno.Value.Trim.Length > 0 AndAlso txtapePaterno.Value.Trim.Length > 0 Then
            Dim jsMensaje As String = Nothing
            Dim pNotaria As Enotaria = Nothing
            If lblTitulo.ToolTip = "Registrar" Then
                pNotaria = New Enotaria
                With pNotaria
                    .descNotaria = txtNotaria.Value.Trim.ToUpper
                    .nomContacto = txtContacto.Value.Trim.ToUpper
                    .apePaterContac = txtapePaterno.Value.Trim.ToUpper
                    .apeMaterContac = txtapeMaterno.Value.Trim.ToUpper
                    .telefono = txtTelefonoFijo.Value.Trim.ToUpper
                    .idUsuario = Me.Master.idUsuario

                End With
                Dim resultado As Boolean = Rnotaria.Añadir(Me.Master.descEmpresa, pNotaria)
                If resultado Then
                    jsMensaje = "<script>javascript:alert(' Se registro correctamente.');</script>)"
                    Controls.Add(New LiteralControl(jsMensaje))
                    cargarnotarias()
                End If

            ElseIf lblTitulo.ToolTip = "Modificar" Then
                pNotaria = New Enotaria
                With pNotaria
                    .idNotaria = CType(txtId.Text, Int32)
                    .descNotaria = txtNotaria.Value.Trim.ToUpper
                    .nomContacto = txtContacto.Value.Trim.ToUpper
                    .apePaterContac = txtapePaterno.Value.Trim.ToUpper
                    .apeMaterContac = txtapeMaterno.Value.Trim.ToUpper
                    .telefono = txtTelefonoFijo.Value.Trim.ToUpper
                    .estado = selEstado.Value
                    .idUsuario = Me.Master.idUsuario
                End With
                Rnotaria.Editar(Me.Master.descEmpresa, pNotaria)
                jsMensaje = "<script>javascript:alert(' Se modifico correctamente.');</script>)"
                Controls.Add(New LiteralControl(jsMensaje))
                cargarnotarias()
            End If
            lblTitulo.ToolTip = "Registrar"
        End If
    End Sub
    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        lblTitulo.ToolTip = "Registrar"
        mpeDetalle.CancelControlID = "btnCancelar"
    End Sub
    Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
        notarias = Session("lista")
        If Not notarias Is Nothing Then
            Dim fileName As String = "Consulta{0}.xls"
            fileName = String.Format(fileName, DateTime.Now.ToShortDateString())
            Dim Response As HttpResponse = HttpContext.Current.Response
            Response.ContentType = "application/vnd.ms-excel"
            Response.Charset = "UTF-8"
            Response.ContentEncoding = System.Text.Encoding.Default
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)

            Response.Write("id")
            Response.Write(vbTab)
            Response.Write("Notaría")
            Response.Write(vbTab)
            Response.Write("Nombre del Contacto ")
            Response.Write(vbTab)
            Response.Write("Apellido Paterno del Contacto")
            Response.Write(vbTab)
            Response.Write("Apellido Materno del Contacto")
            Response.Write(vbTab)
            Response.Write("Telefono")
            Response.Write(vbTab)
            Response.Write("Estado")
            Response.Write(vbTab)
            Response.Write(vbCrLf)

            Dim value As String
            For Each notaria In notarias
                value = notaria.idNotaria
                Response.Write(value)
                Response.Write(vbTab)

                value = notaria.descNotaria
                Response.Write(value)
                Response.Write(vbTab)

                value = notaria.nomContacto
                Response.Write(value)
                Response.Write(vbTab)

                value = notaria.apePaterContac
                Response.Write(value)
                Response.Write(vbTab)

                value = notaria.apeMaterContac
                Response.Write(value)
                Response.Write(vbTab)

                value = notaria.telefono
                Response.Write(value)
                Response.Write(vbTab)

                value = notaria.desEstado
                Response.Write(value)
                Response.Write(vbCrLf)
            Next
            Response.End()
        Else
            Dim jsMensaje As String = Nothing
            jsMensaje = "<script>javascript:alert(' La consulta no pudo ser procesada.  Por favor inténtelo de nuevo.');</script>)"
            Controls.Add(New LiteralControl(jsMensaje))
        End If
    End Sub

End Class
